    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.logic;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.LinkedList;

public class Printer implements Printable {
    public static Printer instance = null;
    
    private Printer() {
        
    }
    
    public void paint (Graphics g) {
        LinkedList<String> list = BuildUpdater.getInstance().getPrintBuild();
        Graphics2D g2d = (Graphics2D) g;
        int x = 30;
        int y = 50;
        for(String s : list) {
            if(s.equals("bold"))
                g2d.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
            else if(s.equals("plain"))
                g2d.setFont(new Font("Comic Sans MS", Font.PLAIN, 12));
            else if(s.equals("nl")) {
                x = 30;
                y += 15;
            } else if(s.equals("indent"))
                x = 100;
            else
                g2d.drawString(s, x, y);
        }
    }
    
    public int print (Graphics g, PageFormat f, int i)
    {
        if (i != 0) // To prevent overflow. i indicates number of pages to print
            return NO_SUCH_PAGE;
        paint (g); // Call paint method
        return PAGE_EXISTS;
    }

    public void printOut() {
        PrinterJob job = PrinterJob.getPrinterJob(); 
        System.out.println ("Staring printer...");
        job.setPrintable(new Printer());
        if (job.printDialog()) {
            try {
                job.print();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static Printer getInstance() {
        if(instance == null)
            instance = new Printer();
        return instance;
    }
}
